package cn.mw.cmdb.convert;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.util.CMDBTool;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static cn.mw.cmdb.entity.ModelInfo.prefix;
import static cn.mw.cmdb.entity.ModelInternalProperty.Brand;
import static cn.mw.cmdb.processor.MonitorValue.InBandIpKey;
import static cn.mw.cmdb.processor.MonitorValue.InStanceNameKey;
import static cn.mw.cmdb.util.ValConvertUtil.strValueConvert;

public class InstanceServiceParamConverter extends BasicServiceParamConverter<InstanceInfo> {
    @Override
    void doConvert(Map src, InstanceInfo dest, ServiceParamConvertContext serviceParamConvertContext) throws Exception {
        //把不属于InstanceInfo成员变量的数据翻到data的map中
        Map dataMap = dest.getData();
        src.putAll(dataMap);
        Iterator<Map.Entry<String, Object>> ite = src.entrySet().iterator();
        //获取map中的数据，注入到instanceInfo中
        if(src.get(InStanceNameKey)!=null){
            String instanceName = strValueConvert(src.get(InStanceNameKey)).trim();
            dest.setInstanceName(instanceName);
        }
        if(src.get(InBandIpKey)!=null){
            String InBandIp = strValueConvert(src.get(InBandIpKey));
            dest.setInBandIp(InBandIp);
        }
        if(src.get(prefix + Brand)!=null){
            String brandId = strValueConvert(src.get(prefix + Brand));
            dest.setBrandId(brandId);
        }
        while (ite.hasNext()) {
            Map.Entry<String, Object> entry = ite.next();
            String key = entry.getKey();
            Set<String> convertedFieldNames = serviceParamConvertContext.getConvertedFieldNames();
            if (!convertedFieldNames.contains(key)) {
                dataMap.put(entry.getKey(), entry.getValue());
            }
        }

    }
}
